package com.harmonyos.zxing.slice;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.ResultPoint;
import com.google.zxing.client.ohos.BeepManager;
import com.harmonyos.zxing.ResourceTable;
import com.google.zxing.journeyapps.barcodescanner.BarcodeCallback;
import com.google.zxing.journeyapps.barcodescanner.BarcodeResult;
import com.google.zxing.journeyapps.barcodescanner.BusHelper;
import com.google.zxing.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.google.zxing.journeyapps.barcodescanner.DefaultDecoderFactory;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ContinuousCaptureAbilitySlice extends AbilitySlice implements Component.ClickedListener, BusHelper.Observable {

    private DecoratedBarcodeView barcodeView;
    private BeepManager beepManager;
    private String lastText;

    private BarcodeCallback callback = new BarcodeCallback() {
        @Override
        public void barcodeResult(BarcodeResult result) {
            if (result.getText() == null || result.getText().equals(lastText)) {
                // Prevent duplicate scans
                return;
            }

            lastText = result.getText();
            barcodeView.setStatusText(result.getText());

            beepManager.playBeepSoundAndVibrate();

            //Added preview of scanned barcode
            Image imageView = (Image) findComponentById(ResourceTable.Id_barcodePreview);
            imageView.setPixelMap(result.getBitmapWithResultPoints(Color.YELLOW));
        }

        @Override
        public void possibleResultPoints(List<ResultPoint> resultPoints) {
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_continuous_capture);

        WindowManager windowManager = WindowManager.getInstance();
        Window window = windowManager.getTopWindow().get();
        window.setTransparent(true);

        barcodeView = (DecoratedBarcodeView) findComponentById(ResourceTable.Id_barcode_scanner);
        Collection<BarcodeFormat> formats = Arrays.asList(BarcodeFormat.QR_CODE, BarcodeFormat.CODE_39);
        barcodeView.getBarcodeView().setDecoderFactory(new DefaultDecoderFactory(formats));
        barcodeView.initializeFromIntent(getAbility().getIntent());
//      barcodeView.decodeContinuous(callback);

        beepManager = new BeepManager(getAbility());

        findComponentById(ResourceTable.Id_pause).setClickedListener(this);
        findComponentById(ResourceTable.Id_resume).setClickedListener(this);

        BusHelper.getINSTANCE().register("CameraPreviewStarted", this);
    }

    @Override
    public void onActive() {
        super.onActive();

        barcodeView.resume();
    }

    @Override
    protected void onInactive() {
        super.onInactive();

        barcodeView.pause();
    }

    @Override
    protected void onStop() {
        super.onStop();
        BusHelper.getINSTANCE().unRegister(this);
    }

    public void triggerScan(Component view) {
        barcodeView.decodeSingle(callback);
    }

    @Override
    public void onClick(Component component) {
        if (component.getId() == ResourceTable.Id_pause) {
            barcodeView.pause();
        } else if (component.getId() == ResourceTable.Id_resume) {
            barcodeView.resume();
        }
    }

    @Override
    public void onSubscribe(Object object) {
        if (barcodeView != null)
            barcodeView.decodeContinuous(callback);
    }
}
